<%@page contentType="text/html;charset=utf-8" pageEncoding="UTF-8" %>
<%@page import="org.ipo.mm.web.util.ConstantManager, java.util.Collection, org.ipo.mm.web.util.security.SecurityManager, org.ipo.mm.web.util.pager.AbstractPager, java.util.Vector" %>
<%@taglib prefix="l" uri="http://struts.apache.org/tags-logic" %>
<%@taglib prefix="b" uri="http://struts.apache.org/tags-bean" %>
<%@taglib prefix="h" uri="http://struts.apache.org/tags-html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<script type="text/javascript">
    function start(){
        startRepresentative();
    }
    function startRepresentative(){
        switchMenu('searchPanelRepresentative');
    }
</script>

<h1><b:message key="menu_representatives"/></h1>
<%
            Collection<Integer> privileges = (Collection<Integer>) request.getSession().getAttribute(ConstantManager.SESSION_USER_PRIVILEGES);
            if (privileges.contains(SecurityManager.CREATE_REPRESENTATIVE_PRIVILEGE)) {%>

<h:link styleClass="createNew" action="/protected/dispatch?method=showCreateReprForm"><b:message key="repr_create"/></h:link>

<%}%>



<!--advanced search form -->
<a class="detailSearch" onclick="switchMenu('searchPanelRepresentative');" href="#"><b:message key="gen_advanced_search"/></a>
<h:link styleClass="email" action="/protected/dispatch?method=showSendMail&clearStack=true"><b:message key="menu_representatives_mails"/></h:link>
<h:link styleClass="printEnvelope" action="/protected/dispatch?method=showPrintPostMail&clearStack=true"><b:message key="menu_representatives_envelopes"/></h:link>
<div id="searchPanelRepresentative">
    <br>
    <h:form  styleId="form3" action="/protected/representative_search">
        <div style="clear:none">
            <div class="detailSearchForm" >
                <em><strong><b:message key="repr_details_label"/></strong></em><br />
                <table border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td><b:message key="repr_name"/></td>
                        <td>
                            <h:text property="nameFilter" styleClass="filter" />
                            <h:select property="nameOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>

                    <tr>
                        <td><b:message key="gen_telephone"/></td>
                        <td>
                            <h:text property="telephoneFilter" styleClass="filter" />
                            <h:select property="telephoneOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="gen_fax"/></td>
                        <td>
                            <h:text property="faxFilter" styleClass="filter" />
                            <h:select property="faxOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="gen_email"/></td>
                        <td>
                            <h:text property="emailFilter" styleClass="filter" />
                            <h:select property="emailOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="repr_function"/></td>
                        <td>
                            <h:text property="functionFilter" styleClass="filter" />
                            <h:select property="functionOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>

                    <tr>
                        <td><b:message key="repr_inst"/></td>
                        <td>

                            <h:select property="institutionFilter" size="5" multiple="true" styleClass="list" >
                                <l:iterate id="inst" name="search_institutions">
                                    <h:option value="${inst.id}">${inst.name}</h:option>
                                </l:iterate>
                            </h:select>

                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="repr_inst_type"/></td>
                        <td>

                            <h:select property="iTypeFilter" size="5" multiple="true" styleClass="list" >
                                <l:iterate id="type" name="search_types">
                                    <h:option value="${type.id}">${type.name}</h:option>
                                </l:iterate>
                            </h:select>
                        </td>

                    </tr>
                    <tr>
                        <td><b:message key="repr_inst_region"/></td>
                        <td>

                            <h:select property="iRegionFilter" size="5" multiple="true" styleClass="list" >
                                <l:iterate id="region" name="search_regions">
                                    <h:option value="${region.id}">${region.name}</h:option>
                                </l:iterate>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="repr_inst_family"/></td>
                        <td>


                            <h:select property="iFamilyFilter" size="5" multiple="true" styleClass="list" >

                                <l:iterate id="family" name="search_families">
                                    <h:option value="${family.id}">${family.name}</h:option>
                                </l:iterate>
                            </h:select>
                        </td>

                    </tr>
                    <tr>
                        <td><b:message key="repr_inst_city"/></td>
                        <td>
                            <h:select property="iCityFilter" size="5" multiple="true" styleClass="list" >
                                <l:iterate id="city" name="search_cities">
                                    <h:option value="${city.id}">${city.name}</h:option>
                                </l:iterate>
                            </h:select>
                        </td>

                    </tr>
                </table>
            </div><!--detailSearchForm-->
        </div>
        <span style="clear:left"><b:message key="gen_search_order_label"/> <h:select property="orderBy">
                <h:option value="1"><b:message key="repr_name"/></h:option>
                <h:option value="2"><b:message key="gen_email"/></h:option>
                <h:option value="3"><b:message key="gen_telephone"/></h:option>
                <h:option value="4"><b:message key="gen_fax"/></h:option>
                <h:option value="5"><b:message key="repr_function"/></h:option>
                <h:option value="6"><b:message key="repr_inst"/></h:option>
            </h:select> <h:submit><b:message key="gen_search"/></h:submit></span>
    </h:form>

</div><!--searchPanel end-->
<!--advanced search form  end-->



<div class="tabela">
    <table width="100%" border="0" >
        <tr>
            <th scope="col" nowrap="true"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=1"><b:message key="repr_name"/></h:link></th>
            <th scope="col"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=2"><b:message key="gen_email"/></h:link></th>
            <th scope="col"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=3"><b:message key="gen_telephone"/></h:link></th>
            <th scope="col"><b:message key="gen_fax"/></th>
            <th scope="col"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=5"><b:message key="repr_function"/></h:link></th>
            <th scope="col"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=6"><b:message key="repr_inst"/></h:link></th>
            <!--<th scope="col"><b:message key="gen_note"/></th>-->
            <th scope="col" nowrap="true"><b:message key="gen_action"/></th>
        </tr>
        <l:empty name="REP_SEARCH_RESULTS">
            <tr class="red1">
                <td colspan="8">
                    <div class="centeredinfo">
                        <img src="<%= request.getContextPath()%>/icons/info.png" align="top"/>
                        <b:message key="gen_no_search_results"/>

                    </div>
                </td>
            </tr>

        </l:empty>
        <l:iterate id="repr" name="REP_SEARCH_RESULTS" scope="request">
            <tr class="red1" ><!--treba da ide red2 u svakom drugom-->
                <td nowrap="true">

                    <%--
                    <!-- SECURITY CONSTRAINT USER CAN EDIT ONLY ENTITIES HE CREATED -->
                    <c:choose>
                        <c:when test="${(USER == repr.user) || (USER.type == USER_TYPE_ADMIN_PA)}">

                            <h:link action="/protected/dispatch?method=showUpdateReprForm"
                                    paramId="rid" paramName="repr" paramProperty="id">
                                <b:write name="repr" property="firstName"/>
                                <b:write name="repr" property="lastName"/>
                            </h:link>
                        </c:when>
                        <c:otherwise>
                            <b:write name="repr" property="firstName"/>
                            <b:write name="repr" property="lastName"/>
                        </c:otherwise>
                    </c:choose>
                    --%>

                    <%
            if (privileges.contains(SecurityManager.EDIT_REPRESENTATIVE_PRIVILEGE)) {
                    %>
                    <h:link styleClass="details" action="/protected/dispatch?method=showUpdateReprForm"
                            paramId="rid" paramName="repr" paramProperty="id">
                        <b:write name="repr" property="firstName"/>
                        <b:write name="repr" property="lastName"/>
                    </h:link>

                    <% } else {%>
                    <b:write name="repr" property="firstName"/>
                    <b:write name="repr" property="lastName"/>
                    <% } %>
                    
                </td>
                <td><b:write name="repr" property="email"/></td>
                <td><b:write name="repr" property="telephone"/></td>
                <td><b:write name="repr" property="fax"/></td>
                <td><b:write name="repr" property="function"/></td>
                <td>${repr.institution.name}</td>
                <!--<td><b:write name="repr" property="note"/></td>-->
                <td nowrap="true">

                    <%-- security measures to restictive
                    <c:if test="${(USER == repr.user) || (USER.type == USER_TYPE_ADMIN_PA)}">
                        <h:link styleClass="details" action="/protected/dispatch?method=showUpdateReprForm"
                                paramId="rid" paramName="repr" paramProperty="id">
                            <b:message key="gen_update"/>
                        </h:link>
                    </c:if>
                    --%>

                   <h:link action="/protected/meeting_search" paramId="rid" paramName="repr" paramProperty="id">
                        <span style="font-weight: bolder">
                            <b:message key="repr_find_meetings"/>
                        </span>

                    </h:link>
                    <%

                if (privileges.contains(SecurityManager.DELETE_REPRESENTATIVE_PRIVILEGE)) {%>

                    <h:link styleClass="delete" action="/protected/dispatch?method=deleteRepresentative"
                            paramId="rid" paramName="repr" paramProperty="id" onclick="return confirm('Are you sure you want to delete this item?/Da li ste sigurni da zelite da izbrisete ovaj entitet?')"
                            ><b:message key="gen_delete"/></h:link>

                    <%}%>


                </td>
            </tr>
        </l:iterate>

        <tr>
            <th scope="col" nowrap="true"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=1"><b:message key="repr_name"/></h:link></th>
            <th scope="col"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=2"><b:message key="gen_email"/></h:link></th>
            <th scope="col"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=3"><b:message key="gen_telephone"/></h:link></th>
            <th scope="col"><b:message key="gen_fax"/></th>
            <th scope="col"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=5"><b:message key="repr_function"/></h:link></th>
            <th scope="col"><h:link action="/protected/representative_search?method=showReprs&clearStack=true&orderBy=6"><b:message key="repr_inst"/></h:link></th>
            <!--<th scope="col"><b:message key="gen_note"/></th>-->
            <th scope="col" nowrap="true"><b:message key="gen_action"/></th>
        </tr>
    </table>
</div><!--tabela-->

<div id="navigation">

    <% AbstractPager pager = (AbstractPager) request.getSession().getAttribute(ConstantManager.SESSION_REP_PAGER);%>

    <c:if test="${REP_SEARCH_SHOW_PREV}">

        <a href="<%= request.getContextPath() + "/protected/representative_search.do?method=showMeetings&p=" + (pager.getPageNumber() - 1)%>" class="previous"><b:message key="gen_previous"/></a>

    </c:if>


    <%-- padajuca lista sa brojevima strana --%>



    <select name="URL" onchange="window.location=this.options[selectedIndex].value">
        <%

                Vector<Integer> pagenumbers = (Vector<Integer>) request.getAttribute("REP_SEARCH_PAGENUMBERS");
                if (pagenumbers != null) {

                    for (int i = 0; i < pagenumbers.size(); i++) {
                        int pageParam = pager.getPageNumber();
                        int pageNumber = pagenumbers.get(i);

                        if (pageParam == pageNumber) {
                            out.write("<option value=\"" + request.getContextPath() + "/protected/representative_search.do?method=showReprs&p=" + pageNumber + "\" selected=\"selected\">" + pageNumber + "</option>");
                        } else {
                            out.write("<option value=\"" + request.getContextPath() + "/protected/representative_search.do?method=showReprs&p=" + pageNumber + "\">" + pageNumber + "</option>");

                        }


                    }
                }

        %>


    </select>




    <%-- end padajuca lista sa brojvima strana --%>


    <c:if test="${REP_SEARCH_SHOW_NEXT}">

        <a href="<%= request.getContextPath() + "/protected/representative_search.do?method=showMeetings&p=" + (pager.getPageNumber() + 1)%>" class="next"><b:message key="gen_next"/></a>
    </c:if>

        <p>
            <b:message key="gen_items_showing"/> <%= pager.getItemInterval() %> <b:message key="gen_items_of_total"/> <%= pager.getResultsTotal() %>
        </p>

</div>
